package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepsitory;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepsitory destinationRepsitory;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Destination destination) {
        destinationRepsitory.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepsitory.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepsitory.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return destinationRepsitory.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationRepsitory.findAll();
    }


    //通过id查询目的地(关联地区)
    @Override
    public List<Destination> querygetDestByRegionId(String rid) {
        //获取区域对象
        Region region = regionService.get(rid);
        //查询出区域对象对应的目的地的集合id
        List<String> ids = region.getRefIds();
        //使用JPA,通过id查询出目的地集合数据
        return destinationRepsitory.findByIdIn(ids);
    }

    //关键字查询和页面数据
    @Override
    public Page<Destination> query(DestinationQuery qo) {
        //创建查询条件
        Query query = new Query();
        //存入关键字和父id条件
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if (StringUtils.hasLength(qo.getParentId())) {
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        } else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        //获取总条数据
        //long total = template.count(query, Destination.class);
        //判空总条数,返回空分页
        //if (total == 0){
        //    return Page.empty();
        //}
        //分页
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize()
                , Sort.Direction.ASC, "_id");
        //存入查询条件中
        //query.with(pageable);
        //查询出页面和关键字数据集合
        //List<Destination> list = template.find(query, Destination.class);
        //返回页面数据
        // return new PageImpl<>(list,pageable,total);

        //使用分页工具类
        return DBHelper.query(query, Destination.class, template, pageable);
    }

    //目的地吐司查询
    @Override
    public List<Destination> queryToasts(String destId) {
        //创建集合存放数据
        List<Destination> list = new ArrayList<>();
        //调用递归
        this.createToasts(list, destId);
        //集合顺序颠倒
        Collections.reverse(list);
        return list;
    }

    //吐司递归
    private void createToasts(List<Destination> DestList, String parentId) {
        //递归前提,能出
        //无id,返回
        if (!StringUtils.hasLength(parentId)) {
            return;
        }
        //查询父id存入集合
        Destination dest = this.get(parentId);
        DestList.add(dest);
        //如果父id还有就重复调用自身,把数据增加到集合中
        if (StringUtils.hasLength(parentId)) {
            createToasts(DestList, dest.getParentId());
        }
    }


    /**
     * 查询目的地
     *
     * @param regionId:区域id
     * @return:目的地
     */
    @Override
    public List<Destination> queryDestByRegionIdforApi(String regionId) {
        List<Destination> list = null;
        //判断是否中国:-1
        if ("-1".equals(regionId)) {
            list = destinationRepsitory.findByParentName("中国");
        } else {
            list = this.querygetDestByRegionId(regionId);
        }
        //遍历子目的地
        for (Destination destination : list) {
            //分页显示5条数据
            Pageable pageable = PageRequest.of(0,5,Sort.Direction.ASC,"_id");
            List<Destination> child = destinationRepsitory.findByParentId(destination.getId(),pageable);
            destination.setChildren(child);
        }
        return list;
    }


    @Override
    public Destination queryByName(String keyword) {
        return destinationRepsitory.findByName(keyword);
    }
}
